package com.harris.double_db.services;

import com.harris.double_db.entity.MyUserOne;
import com.harris.double_db.entity.MyUserTwo;
import com.harris.double_db.mapper.source1.MyOneMapper;
import com.harris.double_db.mapper.source2.MyTwoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: he changjie on 2019/1/3
 * @Description:
 */
@Service
public class UserService {
    @Autowired
    MyOneMapper myOneMapper;
    @Autowired
    MyTwoMapper myTwoMapper;

    /**
     * 使用mybatis的xml进行查询
     * @return
     */
    public Map<String,Object> findAll(){
        List<MyUserOne> ones = myOneMapper.queryAll1();
        List<MyUserTwo> twos = myTwoMapper.queryAll2();
        Map<String,Object> map=new HashMap<>(2);
        map.put("one",ones);
        map.put("two",twos);
        return map;
    }

    /**
     * 使用mybatis的通用mapper进行查询
     * @return
     */
    public Map<String,Object> findAllByBaseMapper(){
        Example exampleOne = new Example(MyUserOne.class);
        Example.Criteria criteriaOne = exampleOne.createCriteria();
        criteriaOne.andBetween("age",10,30);
        List<MyUserOne> myUserOnes = myOneMapper.selectByExample(exampleOne);
        Example exampleTwo = new Example(MyUserTwo.class);
        Example.Criteria criteriaTwo = exampleTwo.createCriteria();
        criteriaTwo.andBetween("age",10,30);
        List<MyUserTwo> myUserTwos = myTwoMapper.selectByExample(exampleTwo);
        Map<String,Object> map=new HashMap<>(2);
        map.put("one",myUserOnes);
        map.put("two",myUserTwos);
        return map;
    }
}
